import types from './types'
import axios from 'axios'
import Rest from '@/service/rest'

export default {
  /**
   * 获取列表
   */
  getRoles ({commit}, params) {
    let url = '/roles/'
    return new Promise((resolve, reject) => {
      axios.get(url, {
        params
      })
      .then(res => {
        let data = res.data
        commit(types.SET_ROLES, data)
        resolve(data)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 获取详情
   */
  getRole ({commit}, {id}) {
    let url = '/roles/role/' + id
    return new Promise((resolve, reject) => {
      axios.get(url, {
      })
      .then(res => {
        commit(types.SET_ROLE, res.data)
        resolve(res.data)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 新增
   */
  postRole ({commit}, role) {
    let url = '/roles/role/'
    return new Promise((resolve, reject) => {
      axios.post(url, role, {})
      .then(res => {
        let item = res.data
        commit(types.ADD_ROLE, item)
        resolve(item)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 编辑
   */
  putRole ({commit}, role) {
    let url = '/roles/role/' + role.id
    return new Promise((resolve, reject) => {
      axios.put(url, role, {})
      .then(res => {
        let item = res.data
        resolve(item)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 删除
   */
  deleteRole ({commit}, {id}) {
    let url = '/roles/role/' + id
    return new Promise((resolve, reject) => {
      axios.delete(url)
      .then(res => {
        commit(types.DELETE_ROLE, id)
        resolve(id)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  filterRoles ({commit}, params) {
    let url = '/roles/filter'
    return new Promise((resolve, reject) => {
      axios.get(url, {
        params
      })
      .then(res => {
        commit(types.SET_ROLE_LIST, res.data)
        resolve(res.data)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  getRolePerms ({commit}, {id}) {
    let url = '/roles/role/' + id + '/perms'
    return new Promise((resolve, reject) => {
      axios.get(url, {
      })
      .then(res => {
        commit(types.SET_ROLE_PERMS, res.data)
        resolve(res.data)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },
  setRolePerms ({commit}, {role, perms}) {
    let url = '/roles/role/' + role + '/perms'
    return new Promise((resolve, reject) => {
      axios.put(url, perms, {})
      .then(res => {
        commit(types.SET_ROLE_PERMS, perms)
        resolve(perms)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },
  addRolePerm ({commit}, {role, perm}) {
    let url = '/roles/role/' + role + '/perms'
    return new Promise((resolve, reject) => {
      axios.post(url, {role, perm}, {})
      .then(res => {
        let item = {role, perm}
        commit(types.ADD_ROLE_PERM, item)
        resolve(item)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  deleteRolePerm ({commit}, {role, perm}) {
    let url = '/roles/role/' + role + '/perms/' + perm
    return new Promise((resolve, reject) => {
      axios.delete(url)
      .then(res => {
        commit(types.DELETE_ROLE_PERM, perm)
        resolve(role)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  }
}
